{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练、测试、单例测试脚本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### --------------预定义-------------\n",
    "- MODEL_NAME：模型名称，联动checkpoint_dir，框架函数名称\n",
    "- PRETRAINED_CHECKPOINT_DIR：预训练模型存储路径\n",
    "- TRAIN_DIR：训练中保存ckpt的目录\n",
    "- eval_log_dir：存储测试结果的目录\n",
    "- DATASET_DIR：训练集数据，此目录存储的是TFRecords\n",
    "- TEST_DATASET_DIR：测试集数据，此目录存储的是TFRecords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# --------------Model-------------\n",
    "MODEL_NAME='resnet_v2_50'\n",
    "# --------------pre-trained checkpoint save path-------------\n",
    "PRETRAINED_CHECKPOINT_DIR='/home/ramsley/workspace/pretrained_ckpt/'+MODEL_NAME\n",
    "# --------------Traning statistic save path-------------\n",
    "TRAIN_DIR='/home/ramsley/workspace/prostate/ckpt4test'\n",
    "# --------------Testing statistic save path-------------\n",
    "eval_log_dir='/home/ramsley/workspace/prostate/eval'\n",
    "# --------------TRANING DATA-------------\n",
    "# DATASET_DIR=/home/ramsley/DataSet/prostate/trainingData/level-1-balance/patch-based-classification/tf-records\n",
    "DATASET_DIR='/home/ramsley/DataSet/prostate/1-3/trainingData/patch-based-classification/tf-records'\n",
    "# DATASET_DIR=/home/ramsley/DataSet/prostate/single/\n",
    "# --------------TESTING DATA-------------\n",
    "# TEST_DATASET_DIR=/home/ramsley/DataSet/prostate/testingData/level-1-balance/patch-based-classification/tf-records\n",
    "TEST_DATASET_DIR='/home/ramsley/DataSet/prostate/1-3/testingData/patch-based-classification/tf-records'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### -------------- Traning -------------\n",
    "- max_number_of_epochs：跑多少个epoch\n",
    "- optimizer：优化器，Adam效果比较好，收敛快且稳定\n",
    "- learning_rate：用pretrain model的情况下至少为0.001或更低，  非预训练的话上限放宽至0.1；若用Adam可以把上限提高到0.1，对于Adam，稍大的学习率可能能取得更好的效果\n",
    "- num_clones：使用的GPU数量\n",
    "- learning_rate_decay_factor：对于一般SGD，每次衰减学习率的50%；对于Adam，此参数形同虚设\n",
    "- learning_rate_decay_type：一般就取这个指数式下降法\n",
    "- num_epochs_per_decay：对于一般SGD，每次衰减学习率的epoch周期；对于Adam，此参数形同虚设（貌似）\n",
    "- weight_decay：正则系数\n",
    "- num_examples：参与测试的总样本数，需要提前指定；当count_num_examples=True，统计的数会覆盖num_examples的值\n",
    "- count_num_examples：是否统计测试集样本总数\n",
    "- checkpoint_exclude_scope：选择性不加载ckpt的某些参数\n",
    "- trainable_scopes：参与训练的参数；若不指定则全部参数均得到更新；例：--trainable_scopes=resnet_v2_50/logits,resnet_v2_50/block3,resnet_v2_50/block4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%run ./train_image_classifier.py\\\n",
    "--train_dir={TRAIN_DIR}/{MODEL_NAME}\\\n",
    "--train_dir={TRAIN_DIR}/{MODEL_NAME} \\\n",
    "--dataset_dir={DATASET_DIR} \\\n",
    "--model_name={MODEL_NAME} \\\n",
    "--max_number_of_epochs=9 \\\n",
    "--batch_size=64 \\\n",
    "--optimizer=adam \\\n",
    "--learning_rate=0.1 \\\n",
    "--num_clones=4 \\\n",
    "--learning_rate_decay_factor=0.5 \\\n",
    "--learning_rate_decay_type=exponential \\\n",
    "--num_epochs_per_decay=3.0 \\\n",
    "--weight_decay=0.00004 \\\n",
    "--num_examples=389915 \\\n",
    "--count_num_examples=False \\\n",
    "--checkpoint_path={PRETRAINED_CHECKPOINT_DIR}/{MODEL_NAME}.ckpt \\\n",
    "--checkpoint_exclude_scopes={MODEL_NAME}/logits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### -------------- Testing -------------\n",
    "- train_log_path：train_log路径，需要读取\n",
    "- is_train_data：train/validation\n",
    "- eval_log_dir：写eval_log_dir的路径\n",
    "- num_examples：参与测试的总样本数，需要提前指定；当count_num_examples=True，统计的数会覆盖num_examples的值\n",
    "- count_num_examples：是否统计测试集样本总数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%run ./eval_image_classifier.py \\\n",
    "--train_log_path={TRAIN_DIR}/{MODEL_NAME}/train_log.txt \\\n",
    "--checkpoint_dir={TRAIN_DIR}/{MODEL_NAME} \\\n",
    "--dataset_dir={TEST_DATASET_DIR} \\\n",
    "--is_train_data=validation \\\n",
    "--eval_log_dir={TRAIN_DIR}/{MODEL_NAME}\\eval \\\n",
    "--batch_size=12 \\\n",
    "--num_examples=146188 \\\n",
    "--count_num_examples=False \\\n",
    "--model_name={MODEL_NAME}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### --------------Single WSI Testing -------------\n",
    "- train_log_path：train_log路径，需要读取\n",
    "- is_train_data：train/validation\n",
    "- eval_log_dir：写eval_log_dir的路径\n",
    "- write_eval_log：是否记录测试日志\n",
    "- single_test_list：需要测试的单例；形式为A,B,...；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%run ./single_eval_image_classifier.py \\\n",
    "--train_log_path={TRAIN_DIR}/{MODEL_NAME}/train_log.txt \\\n",
    "--checkpoint_dir={TRAIN_DIR}/{MODEL_NAME} \\\n",
    "--dataset_dir={TEST_DATASET_DIR} \\\n",
    "--is_train_data=validation \\\n",
    "--eval_log_dir={TRAIN_DIR}/{MODEL_NAME}/eval \\\n",
    "--write_eval_log=True \\\n",
    "--batch_size=256 \\\n",
    "--single_test_list=None \\\n",
    "--model_name={MODEL_NAME} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
